home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 5 code / Lisp Mini-App / Program / draw-dialog-class.lisp < prev    next >
Encoding:
Text File  |  1992-04-08  |  12.1 KB  |  279 lines  |  [TEXT/CCL2]

  1. #|
  2.    draw-dialog-class.lisp
  3.  
  4.    Defines the DRAW-DIALOG class used in the Mini-Application
  5.    sample program.
  6.  
  7.    For further info, see files "About Mini-App" and "Instructions".
  8.  
  9.  
  10.    Copyright 1990, 1991 by Ruben Kleiman for Apple Computer, Inc.
  11.  
  12.    Change History.
  13.    03-12-92 slm  Updated file header comments.
  14.    03-10-92 slm  Create by Rectangle menu item checkmark is now 
  15.                  maintained by view-activate-event-handler. Note
  16.                  that there is no change when another type of 
  17.                  window other than a draw-dialog is activated.
  18.    03-08-92 slm  Changed class of draw-dialog from color-dialog to 
  19.                  dialog because color-dialog is superseded, and
  20.                  want tutorial to run on B&W Macs.
  21.  
  22. |#
  23.  
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;; Define DRAW-DIALOG class
  26. ;;;
  27. ;;;   This class is used to create windows on which we will build objects.
  28. ;;;   The window may be used under two main modes:
  29. ;;;
  30. ;;;            AUTHOR MODE -- It is possible to add objects to the window
  31. ;;;                           from the palette, look at object properties,
  32. ;;;                           and edit object scripts.
  33. ;;;
  34. ;;;            BROWSE MODE -- Only the regular script handlers are activated.
  35. ;;;
  36. ;;;   The slot browse-mode determines which mode the window is in (default: Author mode)
  37. ;;;   
  38. (defclass draw-dialog (window)
  39.   ((my-items              :initform NIL)       ; List of all items in window
  40.    (item-last-under-mouse :initform NIL)       ; Item currently under the mouse
  41.    (browse-mode           :initform nil)       ; Mode in which window is being used (default = author)
  42.    (selections            :initform nil)       ; Currently selected item(s)
  43.    (create-by-rectangle   :initform nil))      ; Can draw-items be created by dragging out a rectangle?
  44.   (:documentation "This class defines our windows"))
  45.  
  46. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  47. ;;; view-activate-event-handler [draw-dialog]
  48. ;;;
  49. ;;; This gets called by MCL whenever the window is about to be activated.
  50. ;;; We want to make sure that the Window Info... and Window Script menu items
  51. ;;; are activated when this window is in the front.
  52. ;;;
  53. (defmethod view-activate-event-handler ((w draw-dialog))
  54.   (menu-item-enable *window-object-info-menu-item*)
  55.   (menu-item-enable *window-script-menu-item*)
  56.   (set-menu-item-check-mark *create-by-rectangle-menu-item* 
  57.                             (slot-value w 'create-by-rectangle))
  58.   (and (slot-value w 'selections)
  59.        (set-menu-title *selected-object-menu-indicator*
  60.                        (concatenate 'string "Selected:  " 
  61.                                     (slot-value (car (slot-value w 'selections)) 'name))))
  62.   (call-next-method))
  63.  
  64. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  65. ;;; view-deactivate-event-handler [draw-dialog]
  66. ;;;
  67. ;;; This gets called by MCL whenever the window is about to be deactivated.
  68. ;;; We want to make sure that the Window Info... and Window Script menu items
  69. ;;; are disabled when this window is no longer in the front.
  70. ;;;
  71. (defmethod view-deactivate-event-handler ((w draw-dialog))
  72.   (menu-item-disable *window-object-info-menu-item*)
  73.   (menu-item-disable *window-script-menu-item*)
  74.   (call-next-method))
  75.  
  76. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  77. ;;; view-draw-contents [draw-dialog]
  78. ;;;
  79. ;;; This will draw the contents of the window in a back to front order
  80. ;;; using the list in the slot MY-ITEMS.
  81. ;;;
  82. (defmethod view-draw-contents ((window draw-dialog))
  83.   (dolist (item (slot-value window 'my-items))
  84.     (view-draw-contents item)))
  85.  
  86. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  87. ;;; window-close [draw-dialog]
  88. ;;;
  89. ;;; This gets called when the window is closed.
  90. ;;; draw-item rectangles are disposed.
  91. ;;;
  92. (defmethod window-close ((w draw-dialog))
  93.   (dolist (item (slot-value w 'my-items))
  94.     (dispose-record (slot-value item 'rectangle) :rect))
  95.   (call-next-method))      ; This will actually close the window
  96.  
  97. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  98. ;;; window-null-event-handler [draw-dialog]
  99. ;;;
  100. ;;; This gets called by MCL after _WaitNextEvent returns a null event
  101. ;;;
  102. (defmethod window-null-event-handler ((w draw-dialog))
  103.   (let* ((where (view-mouse-position w))    ; Window coordinate point of mouse
  104.          (item (find-view-containing-point w (point-h where) (point-v where)))
  105.          (last-under-mouse (slot-value w 'item-last-under-mouse)))
  106.     ;; Handle mouse-within, mouse-enter and mouse-leave events
  107.     (when (and (slot-value w 'browse-mode)   ; in browser mode and
  108.                item)                         ; when mouse is over an item
  109.       (cond ((eq last-under-mouse item)
  110.              (mouse-within item where))
  111.             (t (if last-under-mouse
  112.                  (mouse-leave last-under-mouse where))
  113.                (setf (slot-value w 'item-last-under-mouse)
  114.                      item)
  115.                (mouse-enter item where)))
  116.       ;; Handle idle event for window
  117.       (idle w)))
  118.   (call-next-method))
  119.  
  120. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  121. ;;; view-click-event-handler [draw-dialog]
  122. ;;;
  123. ;;; This gets called by MCL when the mouse goes down in the window.
  124. ;;; We dispatch on MOUSE-DOWN events only when we are in browse mode.
  125. ;;; Otherwise, we call the author mode click event handler to handle
  126. ;;; authoring requirements (moving, resizing objects, etc...)
  127. ;;;
  128. (defmethod view-click-event-handler ((w draw-dialog) where)
  129.   (let ((item (find-view-containing-point w (point-h where) (point-v where))))
  130.     (if (slot-value w 'browse-mode)
  131.       (if item
  132.         (mouse-down item where))
  133.       (author-mode-click-handler item where))))
  134.  
  135. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  136. ;;; window-mouse-up-event-handler [draw-dialog]
  137. ;;;
  138. ;;; This gets called by MCL when the mouse goes up in the window.
  139. ;;; We dispatch on MOUSE-UP events only when we are in browse mode.
  140. ;;;
  141. (defmethod window-mouse-up-event-handler ((w draw-dialog))
  142.   (let* ((where (view-mouse-position w))
  143.          (item (find-view-containing-point w (point-h where) (point-v where))))
  144.     (if (and item
  145.              (slot-value w 'browse-mode))
  146.       (mouse-up item where)))
  147.   (call-next-method))
  148.  
  149. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  150. ;;; view-key-event-handler [draw-dialog]
  151. ;;;
  152. ;;; This gets called by MCL when a key is depressed and the window is selected.
  153. ;;; We dispatch on KEY events only when we are in browse mode
  154. ;;;
  155. (defmethod view-key-event-handler ((w draw-dialog) character)
  156.   (let* ((where (view-mouse-position w))
  157.          (item (find-view-containing-point w (point-h where) (point-v where))))
  158.     (if (and item
  159.              (slot-value w 'browse-mode))
  160.       (key item character)))
  161.   (call-next-method))
  162.  
  163. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  164. ;;; author-mode-click-handler [draw-dialog]
  165. ;;;
  166. ;;; Gets called whenever there is a click in a DRAW-DIALOG window,
  167. ;;; the click was not over an object in the window, and
  168. ;;; the window is in author mode (i.e., not in browse mode)
  169. ;;;
  170. (defmethod author-mode-click-handler ((w draw-dialog) where)
  171.   (if (double-click-p)
  172.     (author-mode-double-click-handler w where)
  173.     (author-mode-single-click-handler w where)))
  174.  
  175. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  176. ;;; author-mode-double-click-handler [draw-dialog]
  177. ;;;
  178. ;;; Gets called when there is a double click on the DRAW-DIALOG window,
  179. ;;; the click was not over an object in the window, and
  180. ;;; the window is in author mode
  181. ;;;
  182. (defmethod author-mode-double-click-handler ((w draw-dialog) where)
  183.   (declare (ignore where))
  184.   ;; Show window information (same as selecting Window Info... menu item)
  185.   (show-object-info w))
  186.  
  187. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  188. ;;; author-mode-single-click-handler [draw-dialog]
  189. ;;;
  190. ;;; Gets called when there is a double click on the DRAW-DIALOG window,
  191. ;;; the click was not over an object in the window, and
  192. ;;; the window is in author mode
  193. ;;;
  194. (defmethod author-mode-single-click-handler ((w draw-dialog) where)
  195.   ;; Deselect selected items if appropriate:
  196.   (unless (find-view-containing-point w (point-h where) (point-v where))
  197.     (deselect-items w))
  198.   
  199.   ;; Check whether the user intends to drag out a rectangle.
  200.   ;; If so, then drag out a grey rectangle and create a draw-item
  201.   ;; if appropiate by checking *clonable-item*.
  202.   (if (and *clonable-item*                       ; Is there a class from which we may create a draw-item?
  203.            (slot-value w 'create-by-rectangle))  ; and can user create draw-item by dragging out a rectangle?
  204.     (multiple-value-bind (topleft bottomright)
  205.                          (select-rectangle w)
  206.       (let ((clone (clone-draw-item *clonable-item*)))
  207.         (set-view-position clone topleft)
  208.         (set-view-size clone (subtract-points bottomright topleft))
  209.         (add-items w clone)))))
  210.  
  211. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  212. ;;; deselect-items [draw-dialog]
  213. ;;;
  214. ;;; Called to deselect everything in window
  215. ;;;
  216. (defmethod deselect-items ((window draw-dialog))
  217.   (dolist (item (slot-value window 'selections))
  218.     (setf (slot-value item 'selected) nil)     ; Turn off selection flag
  219.     (view-draw-contents item))                 ; Redraw item
  220.   (setf (slot-value window 'selections) nil))
  221.  
  222. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  223. ;;; show-info [draw-dialog]
  224. ;;;
  225. ;;; This gets called whenever the window's information box must be shown
  226. ;;;
  227. (defmethod show-info ((w draw-dialog))
  228.   ;; Shows information box for window
  229.   )
  230.  
  231. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  232. ;;; bring-item-to-front [draw-dialog]
  233. ;;;
  234. ;;; This gets called whenever the item (a draw-item) is requested
  235. ;;; to be moved to the front of this window.  The ITEM argument
  236. ;;; is optional: if it is not supplied, then the currently selected
  237. ;;; item, if any, will be brought to the front.
  238. ;;; Being in the front means that it will be drawn last.
  239. ;;; Since items are drawn in order from the beginning to the end of
  240. ;;; of the slot MY-ITEMS of the draw-dialog window, all that needs
  241. ;;; to be done is to move the item to the end of the list.
  242. ;;;
  243. (defmethod bring-item-to-front ((window draw-dialog) &optional item)
  244.   ;; Figure out which item to bring to front, if any:
  245.   (or item (setq item (first (slot-value window 'selections))))
  246.   ;; If there's an item, then bring it to the front:
  247.   (when item
  248.     (setf (slot-value window 'my-items)
  249.           (delete item (slot-value window 'my-items)))
  250.     (setf (slot-value window 'my-items)
  251.           (nconc (slot-value window 'my-items) (list item))))
  252.   ;; Redraw the window so that the change is immediately apparent:
  253.   (view-draw-contents window))
  254.  
  255. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  256. ;;; add-subviews [draw-dialog]
  257. ;;;
  258. ;;; Add new dialog items into our ordered item list after
  259. ;;; they have been added to the dialog.
  260. ;;;
  261. (defmethod add-subviews :after ((d draw-dialog) &rest new-items)
  262.   (dolist (item new-items)
  263.     (pushnew item (slot-value d 'my-items))))
  264.  
  265. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  266. ;;; remove-subviews [draw-dialog]
  267. ;;;
  268. ;;; Remove dialog items from our ordered item list after
  269. ;;; they have been removed from the dialog.
  270. ;;;
  271. (defmethod remove-subviews :after ((d draw-dialog) &rest old-items)
  272.   (dolist (item old-items)
  273.     (setf (slot-value d 'my-items)   (delete item (slot-value d 'my-items)))
  274.     (setf (slot-value d 'selections) (delete item (slot-value d 'selections)))))
  275.  
  276.  
  277. ;end of file draw-dialog-class.lisp
  278. ;------------------------------------------------
  279.